﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public class PhimDAO
    {

        public static int ThemPhim(Phim p)
        {
            int kq;

            SqlConnection cn;
            cn = DataProvider.ConnectionData();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_ThemPhim";
            cmd.Connection = cn;

            SqlParameter para;

            para = new SqlParameter("@TenPhim", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = p.TenPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@GioiThieuPhim", SqlDbType.NVarChar, 255);
            para.Direction = ParameterDirection.Input;
            para.Value = p.GioiThieuPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@ThoiLuongPhim", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = p.ThoiLuongPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@NamSX", SqlDbType.Char, 5);
            para.Direction = ParameterDirection.Input;
            para.Value = p.NamSX;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@NuocSX", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = p.NuocSX;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@HangSX", SqlDbType.NVarChar, 50);
            para.Direction = ParameterDirection.Input;
            para.Value = p.HangSX;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@DSDienVien", SqlDbType.NVarChar, 255);
            para.Direction = ParameterDirection.Input;
            para.Value = p.DsDienVien;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@Poster", SqlDbType.Image);
            para.Direction = ParameterDirection.Input;
            para.Value = p.Poster;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@Trailer", SqlDbType.VarChar, 255);
            para.Direction = ParameterDirection.Input;
            para.Value = p.Trailer;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@BinhChonPhim", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = p.BinhChonPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@MaDinhDangPhim", SqlDbType.Int);
            para.Direction = ParameterDirection.Input;
            para.Value = p.MaDinhDang;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@MaRap", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = p.MaRap;
            cmd.Parameters.Add(para);

            para = new SqlParameter("@MaTheLoaiPhim", SqlDbType.Char, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = p.MaTL1;
            cmd.Parameters.Add(para);

            para = new SqlParameter("kq", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            kq = (int)cmd.Parameters["kq"].Value;
            cn.Close();

            return kq;
        }
        //load thông tin phim (tenphim, madinhdang, thoiluongphim) theo mã phim
        public static List<Phim> LoadThongTinPhim_TheoMaPhim(String MaPhim)
        {
            List<Phim> dsPhim = new List<Phim>();
           
            SqlConnection cn;
            cn = DataProvider.ConnectionData();


            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SP_LOAD_TENPHIM_DINHDANGPHIM_THOILUONGPHIM";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = cn;

            SqlParameter para;
            para = new SqlParameter("MAPHIM", SqlDbType.NVarChar, 100);
            para.Direction = ParameterDirection.Input;
            para.Value = MaPhim;
            cmd.Parameters.Add(para);

            SqlDataReader DataR = cmd.ExecuteReader();
            try
            {
                if (DataR.Read())
                {
                    Phim P = new Phim();
                    P.TenPhim = DataR[0].ToString();
                    P.ThoiLuongPhim = DataR[2].ToString();
                    P.MaDinhDang = DataR[1].ToString();

                    dsPhim.Add(P);

                }
            }
            finally
            {

                if (DataR != null)
                    DataR.Close();

                if (cn != null) 
                    cn.Close();

            }

            return dsPhim;
        }

        //KIỂM TRA MÃ PHIM CÓ ĐÚNG ĐỊNH DẠNG
        public static int KiemTra_MaPhim(string MaPhim)
        {
            SqlConnection cn;
            cn = DataProvider.ConnectionData();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_KIEMTRA_TONTAI_MAPHIM";
            cmd.Connection = cn;

            SqlParameter para;
            para = new SqlParameter("MAPHIM", SqlDbType.NVarChar, 20);
            para.Direction = ParameterDirection.Input;
            para.Value = MaPhim;
            cmd.Parameters.Add(para);

            para = new SqlParameter("KQ", SqlDbType.Int);
            para.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(para);

            cmd.ExecuteNonQuery();
            int KQ = (int)cmd.Parameters["KQ"].Value;
            cn.Close();

            return KQ;
        }
    }
}
